package com.igeek.shop.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.MapListHandler;

import com.igeek.common.utils.DataSourceUtils;
import com.igeek.shop.dao.BaseDao;
import com.igeek.shop.dao.ProductDao;
import com.igeek.shop.entity.Category;
import com.igeek.shop.entity.Order;
import com.igeek.shop.entity.PageBean;
import com.igeek.shop.entity.Product;


public class ProductService {
	private ProductDao dao = new ProductDao();
	
	public List<Product> findHotProductList() {
		// TODO Auto-generated method stub
		List<Product> hotProductList = null;
		try {
			hotProductList = dao.findHotProducts();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hotProductList;
	}
	public List<Product> findNewProductList() {
		// TODO Auto-generated method stub
		List<Product> newProduct = null;
		try {
			newProduct = dao.findNewProductList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newProduct;
	}
	public List<Category> findCategoryList() {
		// TODO Auto-generated method stub
		List<Category> findCate =null;
		try {
			 findCate = dao.findCategoryList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return findCate;
	}
	
	//根据类别查找分页PageBean对象
	
	public PageBean<Product> findProductListByCid(String cid,int currentPage,int currentCount) {
		// TODO Auto-generated method stub
		PageBean<Product> pageBean = new PageBean<>();
		
		//当前页面
		
		pageBean.setCurrentPage(currentPage);
		
		//当前显示的数据条数
		
		pageBean.setCurrentCount(currentCount);
		
		//总共的条数，需要查询数据库
		int totalCount = 0;
		try {
			totalCount = dao.getCount(cid);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pageBean.setTotalCount(totalCount);
		
		//总页数
		int totalPage = (int)Math.ceil(1.0*totalCount/currentCount);
		pageBean.setTotalPage(totalPage);
		
		//当页显示的数据，默认查找第一页，从0开始
		int start = (currentPage-1)*currentCount;
		List<Product> list = null;
		try {
			 list = dao.findProductByCid(cid,start,currentCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pageBean.setList(list);
		
		return pageBean;
	}
	public Product findProductById(String pid) {
		// TODO Auto-generated method stub
		Product product = null;
		try {
			product = dao.findProductById(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return product;
	}
	
	/*
	 * 	提交订单的业务
	 * 因为用到了事务，所以并不能在c3p0中获取对象，应该自己创建对象
	 * */
	public void submitOrders(Order order) {
		// TODO Auto-generated method stub
		//1、开启事务
		try {
			//1.开启事务
			DataSourceUtils.startTransaction();

			//2.调用dao层的操作order的方法
			dao.addOrders(order);
			
			//3.调用dao层的操作orderitem的方法
			dao.addOrderItems(order);
			
		} catch (SQLException e) {
			//事务回滚
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				//提交释放资源
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	//更新订单收货信息
	public void updateOrderInfo(Order order) {
		// TODO Auto-generated method stub
		BaseDao<Order> dao2 = new BaseDao<Order>();
		String sql = "update orders set address = ?,name=?,telephone=? where oid=?";
		Object[] params = {order.getAddress(),order.getName(),order.getTelephone(),order.getOid()};
		dao2.update(sql, params);
	}
	
	//查询指定用户得所有订单(单表查询)
	public List<Order> findAllOrders(String uid) {
		// TODO Auto-generated method stub
		BaseDao<Order> dao = new BaseDao<Order>();
		List<Order> orderList = null;
		String sql = "select * from orders where uid = ?";
		Object[] params = {uid};
		orderList = (List<Order>) dao.getList(sql, params, new Order());
		return orderList;
	}
	public List<Map<String, Object>> findAllOrderItems(String oid) {
		// TODO Auto-generated method stub
		BaseDao<Object> dao = new BaseDao<Object>();
		String sql = "select i.count,i.subtotal,p.pimage,p.pname,p.shop_price from "
				+ "orderitem i,product p where i.pid=p.pid and i.oid=?";
		Object[] params = {oid};
		List<Map<String,Object>> listMap = (List<Map<String, Object>>) dao.getMap(sql, params, new Object());
		return listMap;
	}
}
